package com.fanghuaman.user.controller;

import com.alibaba.fastjson.JSON;
import com.fanghuaman.common.util.SecurityUtil;
import com.fanghuaman.model.common.vo.ResponseResult;
import com.fanghuaman.model.user.dto.RegisterDto;
import com.fanghuaman.model.user.dto.UserEditDto;
import com.fanghuaman.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shiminghui
 * @date 2024/11/17 17:02
 * @description: TODO
 */
@RestController
@RequestMapping
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getuser")
    public ResponseResult getUser() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SecurityUtil.User user = SecurityUtil.getUser(principal.toString());
        return ResponseResult.success(user);
    }

    /**
     * 用户注册
     *
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestBody RegisterDto registerDto) {
        log.info("registerDto:" + JSON.toJSONString(registerDto));
        ResponseResult result = userService.register(registerDto);
        return result;
    }

    @PostMapping("/forget/password")
    public ResponseResult forgetPassword(@RequestBody RegisterDto registerDto) {
        ResponseResult result = userService.forgetPassword(registerDto);
        return result;
    }


    @PostMapping("/edit/{userId}")
    public ResponseResult edit(@RequestBody UserEditDto userEditDto) {
        ResponseResult result = userService.edit(userEditDto);
        return result;
    }

}
